import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layouts/index.vue'

// 整合所有路由并过滤掉 hidden: true 的路由
const routes = [
  {
    path: '/',
    redirect: '/login' // 访问路径是 / 时重定向到 /login
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue'),
    meta: { title: '404' }
  },
  {
    path: '/account/list',
    name: 'AccountList',
    component: () => import('../views/account/List.vue'),
    meta: { title: '账户列表', icon: 'list', roles: ['admin'] }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/welcome',
    children: [
      {
        path: 'welcome',
        name: 'Welcome',
        component: () => import('../views/Welcome.vue'),
        meta: { title: '欢迎页', icon: 'home' }
      },
      {
        path: 'account',
        name: 'Account',
        redirect: '/account/profile',
        meta: { title: '账户管理', icon: 'user' },
        children: [
          {
            path: 'profile',
            name: 'Profile',
            component: () => import('../views/account/Profile.vue'),
            meta: { title: '个人信息', icon: 'profile' }
          },
          {
            path: 'password',
            name: 'Password',
            redirect: '/account/profile',
            meta: { title: '修改密码', icon: 'password' }
          },
          {
            path: 'list',
            name: 'AccountList',
            component: () => import('../views/account/List.vue'),
            meta: { title: '账户列表', icon: 'list', roles: ['admin'] }
          }
        ]
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    meta: { title: '系统管理', icon: 'setting', roles: ['admin'] },
    children: [
      {
        path: 'user',
        name: 'SystemUser',
        component: () => import('../views/system/User.vue'),
        meta: { title: '用户管理', icon: 'user', roles: ['admin'] }
      },
      {
        path: 'role',
        name: 'SystemRole',
        component: () => import('../views/system/Role.vue'),
        meta: { title: '角色管理', icon: 'role', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/product',
    component: Layout,
    redirect: '/product/list',
    meta: { title: '产品管理', icon: 'goods', roles: ['admin', 'auditor'] },
    children: [
      {
        path: 'list',
        name: 'ProductList',
        component: () => import('../views/product/List.vue'),
        meta: { title: '产品列表', icon: 'list', roles: ['admin', 'auditor'] }
      },
      {
        path: 'category',
        name: 'ProductCategory',
        component: () => import('../views/product/Category.vue'),
        meta: { title: '产品分类', icon: 'category', roles: ['admin', 'auditor'] }
      },
      {
        path: 'approval',
        name: 'ProductApproval',
        component: () => import('../views/product/Approval.vue'),
        meta: { title: '产品审核', icon: 'approve', roles: ['admin', 'auditor'] }
      },
      {
        path: 'marketing',
        name: 'ProductMarketing',
        component: () => import('../views/product/Marketing.vue'),
        meta: { title: '营销管理', icon: 'marketing', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/customer',
    component: Layout,
    redirect: '/customer/list',
    meta: { title: '客户管理', icon: 'customer', roles: ['admin', 'credit'] },
    children: [
      {
        path: 'list',
        name: 'CustomerList',
        component: () => import('../views/customer/List.vue'),
        meta: { title: '客户列表', icon: 'list', roles: ['admin', 'credit'] }
      }
    ]
  },
  {
    path: '/loan',
    component: Layout,
    redirect: '/loan/list',
    meta: { title: '贷款管理', icon: 'loan', roles: ['admin', 'credit', 'risk'] },
    children: [
      {
        path: 'list',
        name: 'LoanList',
        component: () => import('../views/loan/List.vue'),
        meta: { title: '贷款列表', icon: 'list', roles: ['admin', 'credit', 'risk'] }
      },
      {
        path: 'apply',
        name: 'LoanApply',
        component: () => import('../views/loan/Apply.vue'),
        meta: { title: '贷款申请', icon: 'apply', roles: ['admin', 'credit'] }
      },
      {
        path: 'approval',
        name: 'LoanApproval',
        component: () => import('../views/loan/Approval.vue'),
        meta: { title: '贷款审批', icon: 'approve', roles: ['admin', 'risk'] }
      }
    ]
  },
  {
    path: '/contract',
    component: Layout,
    redirect: '/contract/list',
    meta: { title: '合同管理', icon: 'contract', roles: ['admin', 'credit'] },
    children: [
      {
        path: 'list',
        name: 'ContractList',
        component: () => import('../views/contract/List.vue'),
        meta: { title: '合同列表', icon: 'list', roles: ['admin', 'credit'] }
      },
      {
        path: 'create',
        name: 'ContractCreate',
        component: () => import('../views/contract/Create.vue'),
        meta: { title: '生成合同', icon: 'create', roles: ['admin', 'credit'] }
      }
    ]
  },
  {
    path: '/repayment',
    component: Layout,
    redirect: '/repayment/plan',
    meta: { title: '还款管理', icon: 'repayment', roles: ['admin', 'finance'] },
    children: [
      {
        path: 'plan',
        name: 'RepaymentPlan',
        component: () => import('../views/repayment/Plan.vue'),
        meta: { title: '还款计划', icon: 'plan', roles: ['admin', 'finance'] }
      },
      {
        path: 'record',
        name: 'RepaymentRecord',
        component: () => import('../views/repayment/Record.vue'),
        meta: { title: '还款记录', icon: 'record', roles: ['admin', 'finance'] }
      },
      {
        path: 'overdue',
        name: 'RepaymentOverdue',
        component: () => import('../views/repayment/Overdue.vue'),
        meta: { title: '逾期管理', icon: 'overdue', roles: ['admin', 'finance'] }
      }
    ]
  },
  {
    path: '/report',
    component: Layout,
    redirect: '/report/business',
    meta: { title: '统计报表', icon: 'report', roles: ['admin'] },
    children: [
      {
        path: 'business',
        name: 'BusinessReport',
        component: () => import('../views/report/Business.vue'),
        meta: { title: '业务报表', icon: 'business', roles: ['admin'] }
      },
      {
        path: 'loan',
        name: 'LoanReport',
        component: () => import('../views/report/Loan.vue'),
        meta: { title: '贷款报表', icon: 'loan', roles: ['admin'] }
      }
    ]
  }
]

// 路由白名单
const whiteList = ['/login']

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  next()
  // const token = localStorage.getItem('token')
  
  // if (token) {
  //   if (to.path === '/login') {
  //     next('/')
  //   } else {
  //     next()
  //   }
  // } else {
  //   if (whiteList.includes(to.path)) {
  //     next()
  //   } else {
  //     next('/login')
  //   }
  // }
})

export default router